package com.pocket.demo.business.controller;

import cn.hutool.core.bean.BeanUtil;
import com.pocket.demo.base.api.PageResult;
import com.pocket.demo.base.api.R;
import com.pocket.demo.base.consts.SecurityConst;
import com.pocket.demo.base.utils.BeanUtils;
import com.pocket.demo.business.controller.vo.UserPageReqVO;
import com.pocket.demo.business.controller.vo.UserRespVO;
import com.pocket.demo.business.controller.vo.UserSaveReqVO;
import com.pocket.demo.business.po.User;
import com.pocket.demo.business.service.UserService;
import com.pocket.demo.core.i18n.MessageSourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户模块", description = "用户管理相关接口")
@Validated
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private MessageSourceService messageSourceService;

    // 【demo-SpringSecurity-5.4】
    @PreAuthorize("hasRole('" + SecurityConst.ROLE_ADMIN + "')")
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping
    public R<PageResult<UserRespVO>> queryByPage(UserPageReqVO userPageReqVO) {
        PageResult<User> userDOPageResult = this.userService.queryByPage(userPageReqVO);
        // 【demo-i18n-2.3】
        return this.messageSourceService.getOkResult(BeanUtils.toBean(userDOPageResult, UserRespVO.class));
    }

    // 【demo-SpringSecurity-6.3】
    @PreAuthorize("hasAnyAuthority('" + SecurityConst.SYSTEM_USER_EDIT + "','" + SecurityConst.SCOPE_PREFIX + SecurityConst.SYSTEM_USER_EDIT + "')")
    @Operation(summary = "通过主键查询单条数据", description = "通过主键查询单条数据")
    @GetMapping("/{id}")
    public R<User> queryById(@PathVariable("id") String id) {
        return this.messageSourceService.getOkResult(this.userService.queryById(id));
    }

    @PreAuthorize("hasAuthority('" + SecurityConst.SYSTEM_USER_ADD + "')")
    @Operation(summary = "新增数据", description = "新增数据")
    @PostMapping
    public R<UserRespVO> add(@RequestBody @Valid UserSaveReqVO userSaveReqVO) {
        UserRespVO userRespVO = BeanUtil.toBean(this.userService.insert(userSaveReqVO), UserRespVO.class);
        return this.messageSourceService.getOkResult(userRespVO);
    }

    @PreAuthorize("hasAuthority('" + SecurityConst.SYSTEM_USER_EDIT + "')")
    @Operation(summary = "编辑数据", description = "编辑数据")
    @PutMapping
    public R<UserRespVO> edit(UserSaveReqVO userSaveReqVO) {
        UserRespVO userRespVO = BeanUtil.toBean(this.userService.edit(userSaveReqVO), UserRespVO.class);
        return this.messageSourceService.getOkResult(userRespVO);
    }


    @PreAuthorize("hasAuthority('" + SecurityConst.SYSTEM_USER_REMOVE + "')")
    @Operation(summary = "删除数据", description = "删除数据")
    @DeleteMapping("/{id}")
    public R<Boolean> deleteById(@Parameter(description = "用户id") @PathVariable("id") String id) {
        return this.messageSourceService.getOkResult(this.userService.deleteById(id));
    }

}

